package cn.glutnn.controller;


import cn.glutnn.entity.OrderDetail;
import cn.glutnn.entity.Orders;
import cn.glutnn.entity.Result;
import cn.glutnn.service.BusinessService;
import cn.glutnn.service.FoodService;
import cn.glutnn.service.OrderDetailService;
import cn.glutnn.service.OrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 订单表(Orders)表控制层
 *
 * @author msh
 * @since 2024-12-12 16:54:12
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Autowired
    private OrdersService ordersService;
    @Resource
    private FoodService foodService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private BusinessService businessService;

    @PostMapping
    public Result<Integer> save(@RequestBody Orders orders) {
        Integer orderId = ordersService.addOrder(orders);
        return Result.success(orderId);
    }

    @GetMapping("/{id}")
    public Result<Orders> getById(@PathVariable Serializable id) {
        Orders orders = ordersService.getById(id);
        return Result.success(orders);
    }

    // 根据用户ID查询订单
    @GetMapping("/listOrdersByUserId/{userId}")
    public Result<List<Orders>> listOrdersByUserId(@PathVariable Integer userId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, userId);
        wrapper.orderByDesc(Orders::getOrderDate);
        List<Orders> ordersList = ordersService.list(wrapper);
        return Result.success(ordersList);
    }

    @GetMapping("/list")
    public Result<List<Orders>> list() {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderDate);
        List<Orders> ordersList = ordersService.list(wrapper);
        return Result.success(ordersList);
    }

    // 修改订单
    @PutMapping
    public Result<String> update(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return Result.success("修改成功");
    }

    // 删除订单
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Serializable id) {
        ordersService.removeById(id);
        return Result.success("删除成功");
    }

    // 取消订单
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable Integer id) {
        ordersService.lambdaUpdate()
                .eq(Orders::getOrderId, id)
                .set(Orders::getOrderState, 3)
                .update();
        OrderDetail orderDetail = orderDetailService
                .getOne(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderId, id));
        foodService.updateStock(orderDetail.getFoodId(), +orderDetail.getQuantity());
        return Result.success("取消成功");
    }

    /**
     * 商家确认收货
     */
    @PostMapping("/confirm")
    public Result confirmOrder(@RequestParam Integer orderId, @RequestParam Integer businessId) {
        // 更新订单状态为已完成(2)
        ordersService.update(new LambdaUpdateWrapper<Orders>()
                .eq(Orders::getOrderId, orderId)
                .set(Orders::getOrderState, 2));

        // 增加商家月销量
        businessService.incrementMonthlySales(businessId);

        return Result.success("确认收货成功");
    }

}

